package JFrame.Admin.UserManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import JFrame.Tools;
import serive.AdminService;
import serive.AdminServiceImpl;

/**
 * @className: AdminModifJFrame
 * @description: TODO 类描述
 * @author: Dong
 * @date: 2021/12/29
 **/
public class ModifyUserJFrame extends JFrame {
    private JLabel l_id, l_name, l_password1, l_password2, l_sex, l_address,l_age, l_idCard,l_telephoneNumber;
    private JTextField t_id, t_name,t_address,t_age,t_idCard,t_telephoneNumber;
    private JPasswordField p_password1, p_password2;
    ButtonGroup bg;
    private JRadioButton r_male, r_female;
    private JButton b_reg, b_reset;
    AdminService adminService = new AdminServiceImpl();


    public ModifyUserJFrame() {
        this.setTitle("快递派送系统_用户修改页面");
        this.setSize(500, 800);
        //页面大小不可变
        setResizable(false);
        init();
        this.setLocationRelativeTo(null);//窗体居中显示
        this.setVisible(true);
    }
    public void init() {
        this.setLayout(new GridLayout(10, 4, 5, 5));
        l_id = new JLabel("请输入要修改信息的账号", JLabel.CENTER);
        l_name = new JLabel("姓名", JLabel.CENTER);
        l_idCard = new JLabel("居民身份证", JLabel.CENTER);
        l_password1 = new JLabel("密码", JLabel.CENTER);
        l_password2 = new JLabel("确认密码", JLabel.CENTER);
        l_sex = new JLabel("性别", JLabel.CENTER);
        l_age = new JLabel("年龄", JLabel.CENTER);
        l_telephoneNumber = new JLabel("电话", JLabel.CENTER);
        l_address = new JLabel("地址", JLabel.CENTER);

        t_id = new JTextField();
        t_name = new JTextField();
        t_address =new JTextField();
        t_age=new JTextField();
        t_idCard=new JTextField();
        t_telephoneNumber=new JTextField();

        p_password1 = new JPasswordField();
        p_password2 = new JPasswordField();

        JPanel p = new JPanel();
        bg = new ButtonGroup();
        r_male = new JRadioButton("男");
        r_female = new JRadioButton("女");
        bg.add(r_male);
        bg.add(r_female);

        b_reg = new JButton("修改");
        b_reg.setBackground(new Color(102,204,255));

        b_reg.addActionListener(new ActionListener() {
            String sex=null;

            @Override
            public void actionPerformed(ActionEvent e) {
                Enumeration<AbstractButton> bgElements = bg.getElements();

                while (bgElements.hasMoreElements()) {
                    AbstractButton btn = bgElements.nextElement();
                    if(btn.isSelected()){
                        sex=btn.getText();
                        break;
                    }
                }
                if (sex == null){
                    JOptionPane.showMessageDialog(null,
                            "请选择性别", "！！！",JOptionPane.WARNING_MESSAGE);
                }else {
                    boolean modify = adminService.modify(t_id.getText(), p_password1.getText(), p_password2.getText(), t_idCard.getText(),
                            sex, Integer.parseInt(t_age.getText()), t_address.getText(), t_name.getText(), t_telephoneNumber.getText());
                    if (modify) {
                        JOptionPane.showMessageDialog(null,
                                "修改成功", "！！！", JOptionPane.WARNING_MESSAGE);

                    } else {
                        JOptionPane.showMessageDialog(null,
                                "修改失败，请输入正确的信息", "！！！", JOptionPane.WARNING_MESSAGE);

                    }
                }
            }

        });
        b_reset = new JButton("重置");
        b_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Tools.emptying(t_id, t_name,t_address,t_age,t_idCard,t_telephoneNumber,p_password1, p_password2);
            }
        });
        b_reset.setBackground(new Color(102,204,255));



        p.add(r_male);
        p.add(r_female);


        Tools.setFonts(18,1,l_id, l_name, l_password1, l_password2, l_sex, l_address,l_age,
                l_idCard,l_telephoneNumber,r_male, r_female, b_reg, b_reset);
        Tools.addComponents(this,l_id,t_id,l_name,t_name,l_password1,p_password1,l_password2,p_password2,
                l_address,t_address,l_sex,p, l_age,t_age, l_idCard,t_idCard,l_telephoneNumber,t_telephoneNumber,
                b_reg,b_reset);



    }

}
